/////////////////////////////////////////////
/// Implementation of the simple template "Tell me * of *"
/////////////////////////////////////////////
sparqlresult=""
//context.sendMessageToUser("ok, I'll try find out this.")

//now = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date())
//context.sendMessageToUser(now)

// fetching the log
log = context.getLog()

// the wildcards
wildcard1 = match.wildcard(bitoflife.chatterbean.Match.Section.PATTERN,1).trim();
wildcard2 = match.wildcard(bitoflife.chatterbean.Match.Section.PATTERN,2).trim();

//FROM = "http://demo.ontowiki.net/model/export/?m=http%3A%2F%2Fbis.ontowiki.net%2F&output=xml"
//FROM = "http://sebastian.dietzold.de/rdf/foaf.rdf"
//FromPart = "FROM <http:xmlns.com/foaf/spec/index.rdf> "
//
// where can we find infos about the subject
//documentQuery = "SELECT DISTINCT ?document "+
//	FromPart+
//	"WHERE{?s ?p1 ?subjectpattern. ?s <http:www.w3.org/2000/01/rdf-schema#seeAlso> ?document."+
//	"FILTER regex(?subjectpattern, '.*"+wildcard2+".*', 'i').}"
//log.info(documentQuery)
//context.sendMessageToUser(documentQuery)
//
//documents = context.queryForTable(documentQuery,null,null)
//FromNamedPart = ""
//documents.each(){
//	res = it
//	log.info(res.getResultCount())
//	res.getResultRows().each(){ 
//		FromNamedPart += "FROM <${it["document"]}> \n"
//		log.info(it["document"])
//	}
//}

valueQuery = "SELECT DISTINCT ?value "+
//	FromPart+
//	FromNamedPart+
	"WHERE {?s ?p1 ?subjectpattern. ?s ?p2 ?value. ?p2 ?property ?propertypattern. FILTER regex(?subjectpattern, '.*"+wildcard2+".*', 'i'). FILTER regex(?propertypattern, '.*"+wildcard1+".*', 'i')}"
log.info(valueQuery)
//context.sendMessageToUser(valueQuery)

counter = 0;
values = context.queryForTable(valueQuery,["ontowiki"],null)
values.each(){
	res = it
	counter = counter + res.getResultCount()
	if (res.getResultCount() > 0) context.sendMessageToUser("From " + res.getStoreName() + ":")
	log.info(res.getResultCount())
	res.getResultRows().each(){ 
		log.info(it["value"])
		context.sendMessageToUser(" * "+it["value"])
	}
}
if(counter == 0) context.sendMessageToUser("Sorry, I can't answer your question.")

